<template>
  <div class="mytext-layout">
      <div class="mytext-nav">
          <div class="nav-container">
              <div class="left-items">
                  <template v-for="(nav, index) in mynavs.filter(item => item.type === 'selection')" :key="index">
                      <div class="nav-item">
                          <span class="nav-label">{{ nav.name }}：</span>
                          <select class="nav-select" v-model="selectedValues[nav.column]">
                              <option 
                                  v-for="option in nav.options" 
                                  :key="option.value" 
                                  :value="option.value"
                                  class="select-option"
                              >
                                  {{ option.label }}
                              </option>
                          </select>
                      </div>
                  </template>
              </div>
              <div class="right-items">
                  <template v-for="(nav, index) in mynavs.filter(item => item.type === 'input')" :key="index">
                      <div class="nav-item">
                          <div class="search-container">
                              <input
                                  type="text"
                                  v-model="searchText"
                                  :placeholder="nav.placeholder"
                                  class="nav-input"
                              >
                              <span class="search-icon">
                                <Icon type="ios-search" />
                              </span>
                          </div>
                      </div>
                  </template>
              </div>
          </div>
      </div>
      <LIST v-model:ListData="textdata" :FuncColumn="columnsInfo" @LoadMore="load_more"/>
  </div>
</template>
<script setup>
import { computed, inject, onMounted, ref, watch } from 'vue'
import { get_texts_by_user_id_keys, fix_text_by_id, get_text_self_by_id, } from "@/api/community/main.js"
import { noreapeat_array_byId} from "@/static/js/array/concat_two_norepeat"
import { useRouter } from"vue-router"
import LIST from "./list.vue"
import { success } from '@/static/js/alert'
const router=useRouter()
const community = inject('community')()
const user= inject('user')()
const delete_text = async (row) => { 
  const judge = await fix_text_by_id(user.value.get_user()?.id, row.id,0)
  if (judge == 200) {
     // 删除成功
     success('删除成功')
     community.value.pop_my_text_info(row.id)
  }
}
// 渲染文本信息,可以将编辑和渲染放在一起
const show_text_details = async (id) => {
  const to = router.resolve({ name: "community_render_text", query: { id: id } })
  window.open(to.href, '_blank')
}

const get_text_data_info = async (row) => {
  const history_data=community.value.get_my_text_info_all(row.id)
  if (history_data) { 
    //数据中有数据,直接用
      return history_data
  } else { 
    const data = await get_text_self_by_id(row.id)
    community.value.set_my_text_info_all(row.id, data)
    if (data != 0) { 
      return data
    }
  }
}


const columnsInfo = [
     // 这个是是否需要dom是list的单元的行级别的弹出效果
      // {
      //   // label: '编辑',
      //   // value: 'edit',
      //   // onclick: (row) => {
      //   //   console.log(row)
      //   // }
      // },
      {
        label: '删除',
        value: 'delete',
        onclick: (row) => {
          delete_text(row)
        }
      }, {
        label: '数据',
        value: 'datainfo',
        DOM: './textdatainfo.vue',
        DomStyle: {

        },
        onclick: (row) => {
          return get_text_data_info(row)
        }
  }, {
    label: '浏览',
    value: 'view',
    onclick: (row) => {
      show_text_details(row.id)
    }
  }
]

const timer = ref(null)
const searchText = ref('')     //实际是在分类的前提下进行处理,只不过从服务器获取刷新数据
const mynavs = [
  {
      name: '来源',
      type: 'selection',
      column: 'resource',
      options: [
          { 'label': '全部', 'value': null },
          { 'label': '转载', 'value': 'repost' },
          { 'label': '原创', 'value': 'original' },
          { 'label': '翻译', 'value': 'translation' }
      ]
  },
  {
      name: '分类',
      type: 'selection',
      column:'kind',
      options: [
          { label: '不限',value: null },
          { label: '技术', value: 'tech' },
          { label: '生活', value: 'life' },
          { label: '设计', value: 'design' },
          { label: '阅读', value: 'reading' },
          { label: '旅行', value: 'travel' },
          { label: "美食", value: "food" }
      ]
  }, 
  {
      name: '时间',
      type: 'selection',
      column:'date',
      options: [
          { 'label': '不限', 'value': null },
          { 'label': '最近一周', 'value': 'week' },
          { 'label': '最近一月', 'value': 'month' },
          { 'label': '最近一年', 'value': 'year' }
      ]
  }, 
  {
      name: 'search',
      type: 'input',
      placeholder: '搜索',
      options: []
  }
]
const JSONSTRINGIFY = (data) => {
  let middleString=searchText.value
  Object.keys(data).forEach(key => {
      middleString += `${key}:${data[key]},`
  })
  return JSON.stringify(middleString)
}
const selectedValues = ref(
  mynavs
      .filter(item => item.type === 'selection')
      .reduce((acc, item) => {
          acc[item.column] = item.options[0].value;
          return acc;
      }, {})
)
const textdata =ref([])
const middletextdata = computed(() => { 
  return community.value.get_my_text_info(JSONSTRINGIFY(selectedValues.value))
})
watch(() => middletextdata.value, (newVal) => {
    textdata.value = newVal
}, {deep:true})


watch(() => searchText.value, (newVal) => { 
  const history_data=community.value.get_my_text_info(JSONSTRINGIFY(newVal))
  if (history_data.length > 0) { 
    // 检测到缓存先下发缓存
    textdata.value = history_data
    return
  }
  if (timer.value) {
     clearTimeout(timer.value)
  }
  timer.value = setTimeout(async () => {
      load_more()
  }, 1500);
})
  

watch(() => selectedValues.value, (newVal) => {
  const history_data=community.value.get_my_text_info(JSONSTRINGIFY(newVal))
  if (history_data.length > 0) { 
    // 检测到缓存先下发缓存
    textdata.value = history_data
    return
  }
  if (timer.value) {
     clearTimeout(timer.value)
  }
  timer.value = setTimeout(async () => {
      load_more()
  }, 1500);
}, {deep:true,})

const load_more = async () => {
    //加载数据
    const column = JSONSTRINGIFY(selectedValues.value)
    const pre_meta = community.value.get_my_text_info_meta(column)
  
    if (pre_meta != undefined) {
        
    } else { 
      if ((pre_meta?.limit || 1) * 20 >= pre_meta?.total_count) { 
        //该类已经没有可获得的新数据了
          success('已经没有更多数据了')
          return
      }
    }
    
    const judge =await get_texts_by_user_id_keys(
      user.value.get_user()?.id,
      selectedValues.value,
      searchText.value,
      community.value.get_my_text_info_meta(column)?.limit + 1 || 1,
  )
  if (judge != 0) { 
          const middledata = noreapeat_array_byId(
          community.value.get_my_text_info(column),
          judge.results,
          'id')
          community.value.set_my_text_info(column, middledata)
          community.value.set_my_text_info_meta(column, judge.meta)
    }
}
onMounted(async () => { 
    await load_more()   
})
</script>

<style scoped>
.mytext-layout {
  width: 100%;
  height: 100%;
  overflow: scroll;
}
.mytext-layout::-webkit-scrollbar {
  display: none;
}

.mytext-nav {
  width: 100%;
 
  min-height: 60px; 
  height: auto;     
  overflow: visible;
  padding: 10px 20px;
  background-color: #ffffff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
}

.nav-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  gap: 20px;
}
.select-option:hover{
  background-color: #fff;
}

.left-items {
  display: flex;
  align-items: center;
  gap: 20px; 
  flex-wrap: wrap; 
  flex-grow: 1; 
  min-width: 0; 
}

.right-items {
  display: flex;
  align-items: center;
  flex-shrink: 0; 
}

.nav-item {
  display: flex;
  align-items: center;
  position: relative; 
}

.nav-label {
  font-size: 14px;
  color: #555;
  margin-right: 8px;
  white-space: nowrap;
}

.nav-select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  color: #333;
  background-color: #fff;
  min-width: 120px;
  cursor: pointer;
  appearance: none; 
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 14px; 
  transition: border-color 0.3s;
}

.nav-select:focus {
  outline: none;
  border-color: #4a90e2;
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
}

.nav-select option { 
  background-color: white;
  color: #333;
}
.nav-select option:hover,
.nav-select option:focus,
.nav-select option:checked {
  background-color: #f0f0f0 ; 
  color: #333;
}

.search-container {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%; 
}

.nav-input {
  padding: 8px 12px 8px 35px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  color: #333;
  width: 200px; 
  box-sizing: border-box; 
  transition: border-color 0.3s;
}

.nav-input:focus {
  outline: none;
  border-color: #4a90e2;
  box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 16px; 
  pointer-events: none; 
}

/* 响应式设计 */
@media (max-width: 768px) {
  .mytext-nav {
    padding: 10px; 
  }
  .nav-container {
      flex-wrap: wrap;
      align-items: center;
      gap: 15px; 
    
  }
  
  .left-items {
      gap: 15px; 
  }
  
  .right-items {

      flex-grow: 1; 
      min-width: 200px;
  }
  
  .nav-select {
      min-width: 100px; 
      flex-grow: 1; 
  }
  .nav-item { 
   
  }
  
  .nav-input {
      width: 100%; 
  }
}

@media (max-width: 480px) {
    .left-items, .right-items {
        width: 100%; 
        flex-grow: 0; 
        justify-content: flex-start; 
    }
    .left-items {
    
    }
    .left-items .nav-item {
       
    }
    .nav-select {
        width: 100%; 
    }
}
</style>